import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import local_get from '@dfeidao/atom-web/local/get';
import fire_to from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
// import whoami from '@dfeidao/atom-web/msg/whoami';
import msg_error from '@dfeidao/atom-web/msgbox/error';
import msg_success from '@dfeidao/atom-web/msgbox/success';
import storage_get from '@dfeidao/atom-web/storage/get';
import a002 from './a002';
// 保存
export default async function a008(fd: IFeidaoAiBrowserComponent) {
	// 客户端类型
	const client_type: string = local_get(fd, "client_type");
	let group_number: string = local_get(fd, "group_number");
	let group_name = fd.data.node.querySelector<HTMLSelectElement>('[data-feidao-id="group_number"]').selectedOptions[0].innerHTML;
	if (group_name === "未分组") {
		group_name = "";
		group_number = "";
	}
	// console.log(group_name);
	// 功能名称
	const formname = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-id="formname"]').value.trim();
	// 页面
	const pub_package = local_get(fd, "pub_package") as { no: string, name: string, file_url: string };
	// 功能控制点
	const data = local_get(fd, "data");
	if (!formname) {
		msg_error("请填写功能名称");
		return;
	}
	if (!pub_package) {
		msg_error("请选择一个页面");
		return;
	}
	// 构建参数
	const filter1 = {} as {
		group_name: string;
		formname: string;	// 功能名称
		package_no: string; // 页面编号
		package_name: string;// 页面名称”：弹窗7返回参数;
		package_path: string; // 页面地址：弹窗7返回参数;
		group_number: string;
	};
	filter1.formname = formname;
	filter1.group_name = group_name;
	filter1.group_number = group_number;
	filter1.package_no = pub_package.no;
	filter1.package_name = pub_package.name;
	filter1.package_path = pub_package.file_url;
	// 调用查询服务
	// const user = await whoami();
	const prid = storage_get('productid');
	let inst_obj = {};
	if (data) {
		inst_obj = { filter1, pub_form_code: data, productid: prid, client_type };
	} else {
		inst_obj = { filter1, productid: prid, client_type };
	}
	const res = await nodejs('function-definition/zj-000003/s001', inst_obj);
	if (res === "repeat") {
		msg_error("功能名称重复，请重新输入");
	}
	if (res === "fail") {
		msg_error("添加失败");
	}
	if (res === "success") {
		msg_success("成功添加");
		fire_to('zj-000001', 'a002');
		await a002(fd);
	}
}
